/*
 * Copyright (C) 2014 STNet,Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.co.stnet.apps.medcloud.web.api.resources;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import jp.co.stnet.apps.medcloud.core.exception.BusinessException;
import jp.co.stnet.apps.medcloud.core.message.ResultMessages;
import jp.co.stnet.apps.medcloud.domain.beans.FacilityInformation;
import jp.co.stnet.apps.medcloud.domain.beans.PharmacyListRequest;
import jp.co.stnet.apps.medcloud.domain.beans.PharmacyListResponse;
import jp.co.stnet.apps.medcloud.domain.services.api.PharmacyListResourceService;
import jp.co.stnet.apps.medcloud.web.common.CreateResult;
import jp.co.stnet.apps.medcloud.web.common.ErrorMessage;

/**
 *
 * @author stnetadmin
 */
@Path("/member/pharmacyList")
@Service
public class PharmacyListResource {

    private static final Logger logger = LoggerFactory.getLogger(PharmacyListResource.class);

    @Autowired
    private PharmacyListResourceService pharmacyListResourceService;

    @Autowired
    private MessageSource messageSource;

    /**
     * 薬局リストを返します.
     *
     * @param request PharmacyListRequest オブジェクト
     * @return PharmacyListResponse オブジェクト
     */
    @POST
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    public PharmacyListResponse getPhamacyList(PharmacyListRequest request) {
        List<FacilityInformation> list = pharmacyListResourceService
                .getFacilityInformationList(request);

        ResultMessages messages;

        PharmacyListResponse res = new PharmacyListResponse();
        res.setFacilityInformationList(list);

        // 正常系メッセージを生成
        // 検索結果が空ならエンティティボディが無いため、204 を返す
        messages = ResultMessages.success().add("e.md.fw.0200", messageSource);
        res.setResult(CreateResult.create(messages));

        if (false) {
            messages = ResultMessages.error().add("e.md.fw.9001", messageSource);
            ErrorMessage.create(messages);
            throw new BusinessException(messages);
        } else {
            return res;
        }
    }

}
